package Operation;

import Book.BookList;
import Book.Book;

import java.util.Scanner;

public class DelBook implements IOperation {
    @Override
    public void work(BookList bookList) {
        assert bookList.getBooks()[0] != null;
        System.out.println("删除图书.....");
        Scanner sc = new Scanner(System.in);
        int count = bookList.getUsedSize();
        System.out.println("请输入你要删除书籍的名字：");
        String name = sc.nextLine();
        for (int i = 0; i < count; i++) {
            //寻找书是否存在
            if (bookList.getBooks()[i].getName().equals(name)) {
                bookList.getBooks()[i] = null;
                for (int j = i; j < count - 1; j++) {
                    Book book = bookList.getBooks()[j + 1];
                    bookList.setBooks(j, book);
                }
                bookList.setBooks(count - 1, null);
                bookList.setUsedSize(count - 1);
                System.out.println("删除成功！！！");
                return;
            }
        }
        System.out.println("未找到这本书，无法删除！！！");
    }
}
